$(document)
		.ready(
				function() {
					// 全局的早餐，午餐，晚餐的list
					var breakfastList = new Array();
					var lunchList = new Array();
					var dinnerList = new Array();
					$("#inputTotalPrice").val(0);
					
					// 选择菜品的按钮点击事件
					$("#chooseFoodButton").on("click", function() {
						chooseFoodDetail(breakfastList);
					});

					// 提交生产计划的按钮点击事件
					$("#submitPlanButton")
							.on(
									"click",
									function() {
										var data = {
											"productTime" : $(
													"#inputProductTime").val(),
											"createById" : $("#inputCreateById")
													.val(),
											"remark" : $("#inputRemark").val(),
											"totalPrice" : $("#inputTotalPrice").val(),
											"breakfastList" : JSON
													.stringify(breakfastList),
											"lunchList" : JSON
													.stringify(lunchList),
											"dinnerList" : JSON
													.stringify(dinnerList)
										};
										baseUtil
												.httpSend(
														"productionPlan/createNewProductionPlanAjax",
														data, showResult);
									});

					var breakfastVue = new Vue({
						el : '#breakfastData',
						data : {
							items : null
						},
						methods : {
							onclick : function(food, num, mealTime) {
								openDeletePlanFoodConfirm(food, num, mealTime);
							}
						}
					});

					var lunchVue = new Vue({
						el : '#lunch',
						data : {
							items : null
						},
						methods : {
							onclick : function(food, num, mealTime) {
								openDeletePlanFoodConfirm(food, num, mealTime);
							}
						}
					});

					var dinnerVue = new Vue({
						el : '#dinner',
						data : {
							items : null
						},
						methods : {
							onclick : function(food, num, mealTime) {
								openDeletePlanFoodConfirm(food, num, mealTime);
							}
						}
					});

					function showResult(result) {
						if (result == 0) {
							layer.msg('创建失败!');
						} else {
							layer.alert('创建成功', {
								skin : 'layui-layer-molv' // 样式类名
								,
								closeBtn : 0
							}, function() {
								$("#contentIframe", parent.document.body).attr(
										"src",
										JSP_PRE_URL
												+ "editProductionPlan");
							});
						}

					}

					function chooseFoodDetail() {
						var chooseFoodBox = layer
								.open({
									type : 2,
									maxmin : true,
									title : '选择菜品',
									offset : '4%',
									area : [ '70%', '92%' ],
									btn : [ '确认', '取消' ] // 只是为了演示
									,
									yes : function() {
										// 获得子窗口的值
										var childBox = layer.getChildFrame(
												'body', chooseFoodBox);
										// 新添加的菜品
										var breakfast = childBox.find(
												"#breakfastList").val();
										var lunch = childBox.find("#lunchList")
												.val();
										var dinner = childBox.find(
												"#dinnerList").val();
										if (breakfast != "") {
											breakfast = eval('(' + breakfast
													+ ')');
											for ( var i in breakfast) {
												breakfastList
														.push(breakfast[i]);
											}
										}
										if (lunch != "") {
											lunch = eval('(' + lunch + ')');
											for ( var i in lunch) {
												lunchList.push(lunch[i]);
											}
										}
										if (dinner != "") {
											dinner = eval('(' + dinner + ')');
											for ( var i in dinner) {
												dinnerList.push(dinner[i]);
											}
										}
										// 绑定数据
										Vue.set(breakfastVue, "items",
												breakfastList);
										Vue.set(lunchVue, "items", lunchList);
										Vue.set(dinnerVue, "items", dinnerList);

										updateTotalPrice();
										layer.close(chooseFoodBox);
									},
									btn2 : function() {
										layer.close();
									},
									shadeClose : true, // 点击遮罩关闭
									content : SERVER_PRE_URL+'classification/getAllClassificationReturnTo?returnTo=chooseFood'
								});
					}
					//更新生产成本
					function updateTotalPrice(){
						var totalPrice = 0;
						for ( var i in breakfastList) {
							totalPrice = totalPrice + breakfastList[i].totalPrice;
						}
						for ( var i in lunchList) {
							totalPrice = totalPrice + lunchList[i].totalPrice;
						}
						for ( var i in dinnerList) {
							totalPrice = totalPrice + dinnerList[i].totalPrice;
						}
						
						$("#inputTotalPrice").val(totalPrice);
						
					}
					
					

					function openDeletePlanFoodConfirm(food, num, mealTime) {
						layer.confirm('确认删除:' + food.foodName, {
							btn : [ '确认', '取消' ]
						// 按钮
						}, function() {
							if (mealTime == 1) {
								breakfastList.splice(num, 1);
								Vue.set(breakfastVue, "items", breakfastList);
							} else if (mealTime == 2) {
								lunchList.splice(num, 1);
								Vue.set(lunchVue, "items", lunchList);
							} else {
								dinnerList.splice(num, 1);
								Vue.set(dinnerVue, "items", dinnerList);
							}
							updateTotalPrice();
							layer.msg('删除成功!');
						}, function() {

						});
					}

				});
